package com.example.job_backend.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class EmailSendUtils {
    @Value("${spring.mail.username}")
    private String fromEmail;

    @Autowired
    private JavaMailSender mailSender;

    public void sendResume(String toEmail, String subject, String content, File attachmentFile)
            throws MessagingException {

        if (attachmentFile == null || !attachmentFile.exists()) {
            throw new IllegalArgumentException("附件文件不存在");
        }

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(fromEmail);
        helper.setTo(toEmail);
        helper.setSubject(subject);
        helper.setText(content);

        // 添加附件（使用文件资源）
        helper.addAttachment("简历.docx", new FileSystemResource(attachmentFile));

        mailSender.send(message);
    }
}